package cn.sdhuijin.healthexam.activity;

import java.util.Properties;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import cn.sdhuijin.healthexam.DatabaseHelper;
import cn.sdhuijin.healthexam.ExitApplication;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.control.AbstractControlActivity;
import cn.sdhuijin.healthexam.util.Constants;
import cn.sdhuijin.healthexam.util.ReadWriteProperties;

public class PhzBodyJudgeActivity extends AbstractControlActivity implements
		OnClickListener {

	private String createDate;

	private DatabaseHelper databaseHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_body_judge_pinghezhi);
		ExitApplication.getInstance().addActivity(this);
		databaseHelper = new DatabaseHelper(this);

		Intent intent = getIntent();
		Bundle bundle = intent.getBundleExtra("bundle");
		if (bundle != null) {
			createDate = bundle.getString("createDate");
		}
		setCurrentEditUserName(createDate);

		addRadioGroupView(R.id.rad_phz_1, "平和质充沛");
		addRadioGroupView(R.id.rad_phz_2, "平和质疲乏");
		addRadioGroupView(R.id.rad_phz_3, "平和质无力");
		addRadioGroupView(R.id.rad_phz_4, "平和质低沉");
		addRadioGroupView(R.id.rad_phz_5, "平和质寒冷");
		addRadioGroupView(R.id.rad_phz_6, "平和质变化");
		addRadioGroupView(R.id.rad_phz_7, "平和质失眠");
		addRadioGroupView(R.id.rad_phz_8, "平和质健忘");

		setButtonListener(this, R.id.btn_complete, R.id.btn_previous);

		controlManager.setPropertiesPath(Constants.PROPERTIES_PATH + createDate
				+ ".txt");
		controlManager.toControl();
	}

	/**
	 * 按钮点击处理事件
	 */
	@Override
	public void onClick(View arg0) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		// 获取点击按钮ID
		int id = (Integer) arg0.getId();
		switch (id) {
		case R.id.btn_complete:
			controlManager.toProperties();
			// 保存平和质转化分到properties文件
			saveBodyJudgeScore(createDate, "平和质", R.id.rad_phz_1,
					R.id.rad_phz_2, R.id.rad_phz_3, R.id.rad_phz_4,
					R.id.rad_phz_5, R.id.rad_phz_6, R.id.rad_phz_7,
					R.id.rad_phz_8);
			Properties properties = new ReadWriteProperties()
					.getBodyJudgeFeatureProperties(createDate);
			showBodyJudgeInfoDialog(properties);
			// 更新索引表
			databaseHelper.updateIndexTable(createDate);
			break;
		case R.id.btn_previous:
			controlManager.toProperties();
			intent.setClass(this, TbzBodyJudgeActivity.class);
			bundle.putString("createDate", createDate);
			intent.putExtra("bundle", bundle);
			startActivity(intent);
			finish();
			break;
		default:
			break;
		}
	}

	private void showBodyJudgeInfoDialog(Properties properties) {
		if (properties == null) {
			toastInformation(R.string.toast_body_judge_error);
			Intent intent = new Intent();
			// 跳转新建档案页面
			intent.setClass(PhzBodyJudgeActivity.this, IndexActivity.class);
			startActivity(intent);
			PhzBodyJudgeActivity.this.finish();
		} else {
			new AlertDialog.Builder(PhzBodyJudgeActivity.this)
					.setIcon(android.R.drawable.ic_dialog_info)
					.setTitle("中医体质自测结果")
					.setMessage(
							"体质类型：" + properties.getProperty("体质类型").toString()
									+ "\n" + "体质特征："
									+ properties.getProperty("体质特征").toString()
									+ "\n" + "调节建议："
									+ properties.getProperty("调节建议").toString())
					.setPositiveButton(
							this.getResources().getString(R.string.btn_ok),
							new DialogInterface.OnClickListener() {
								// 点击确定按钮
								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									Intent intent = new Intent();
									// 跳转新建档案页面
									intent.setClass(PhzBodyJudgeActivity.this,
											IndexActivity.class);
									startActivity(intent);
									// 关闭对话框
									arg0.dismiss();
									PhzBodyJudgeActivity.this.finish();
								}
							}).show();
		}
	}
}
